package edu.scau.mis.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.mis.cloth.entity.GsAttrs;
import edu.scau.mis.cloth.entity.GsStorage;
import edu.scau.mis.cloth.service.GsAttrsService;
import edu.scau.mis.cloth.service.GsClothesService;
import edu.scau.mis.cloth.entity.GsClothes;
import edu.scau.mis.cloth.service.GsStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * (GsClothes)表控制层
 *
 * @author CJW
 * @since 2022-03-05 23:35:31
 */
@RestController
@RequestMapping("gsClothes")
public class GsClothesController {
    /**
     * 服务对象
     */
    @Autowired
    private GsClothesService gsClothesService;
    @Autowired
    private GsAttrsService gsAttrsService;


//    根据类别分页显示商品信息
    @GetMapping("/listByCate")
    public PageInfo queryByCate(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestParam("cateId") Integer cateId) {
        PageHelper.startPage(pageNum,pageSize);
        List<GsClothes> list = this.gsClothesService.queryByCate(cateId);
        PageInfo<List> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * 分页查询所有商品信息
     *
     * @param gsClothes 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public PageInfo queryAll(GsClothes gsClothes,@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<GsClothes> list = this.gsClothesService.queryAll(gsClothes);
        PageInfo<List> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * 通过服装id查询商品
     *
     * @param clId 主键
     * @return 单条数据
     */
    @GetMapping("{clId}")
    public ResponseEntity<List<GsClothes>> queryById(@PathVariable("clId") Integer clId) {
        return ResponseEntity.ok(this.gsClothesService.queryById(clId));
    }

    /**
     * 新增商品
     *
     * @param gsClothes 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Boolean> add(@RequestBody GsClothes gsClothes) {
        //cl_id已经存在时不能新增，通过clId查询商品得到一个数组，如果长度不等于0说明已经存在这个clId的商品了，不能继续添加，返回false
        if(this.gsClothesService.queryById(gsClothes.getClId()).size()!=0) return ResponseEntity.ok(false);
        this.gsClothesService.insert(gsClothes);
        return ResponseEntity.ok(true);
    }

    /**
     * 编辑商品信息
     *
     * @param gsClothes 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<GsClothes> edit(@RequestBody GsClothes gsClothes) {
        return ResponseEntity.ok(this.gsClothesService.update(gsClothes));
    }

    /**
     * 删除数据
     *
     * @param clId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete/{clId}")
    public ResponseEntity<Boolean> deleteById(@PathVariable Integer clId) {
        this.gsAttrsService.deleteById(clId);
        return ResponseEntity.ok(this.gsClothesService.deleteById(clId));
    }

    @GetMapping("/page")
    public PageInfo page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, GsClothes gsClothes) {
        PageHelper.startPage(pageNum,pageSize);
        List<GsClothes> list = this.gsClothesService.queryAll(gsClothes);
        PageInfo<List> pageInfo = new PageInfo(list);
        return pageInfo;
    }
}
